package com.juyu.miliao.api.controller;

import com.juyu.miliao.api.constant.EnergyLevelType;
import com.juyu.miliao.api.domain.Appkey;
import com.juyu.miliao.api.domain.vo.FindDynamicLikeVO;
import com.juyu.miliao.api.service.AppKeyService;
import com.juyu.miliao.api.service.EnergyLevelService;
import com.juyu.miliao.api.service.FindDynamicLikeService;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * 发现点赞控制器
 *
 * @author xuanqicheng
 */
@RestController
@RequestMapping("/find/dynamic/like")
public class FindDynamicLikeController {

    @Autowired
    private AppKeyService appKeyService;

    @Autowired
    private EnergyLevelService energyLevelService;

    @Autowired
    private FindDynamicLikeService findDynamicLikeService;

    @PostMapping("/")
    public Object addFindDynamicLikeInfo(Authentication token, int findId) throws IOException {
        Subject subject = (Subject) token.getPrincipal();
        if (findId == 0 && findId !=-1) {
            throw new ConsumeRuntimeException("id不能为空");
        }
        Long userId = subject.getId();

        Appkey energyKey = appKeyService.getAppKeyByKey("likeDynamicObtainEnergy");
        FindDynamicLikeVO findDynamicLikeVO = findDynamicLikeService.addFindDynamicLikeInfo(userId, findId);
        energyLevelService.addUserEnergyLevel(subject.getId(),
                EnergyLevelType.ENERGY_TYPE_GIVE, Integer.parseInt(energyKey.getValue()));
        return findDynamicLikeVO;
    }
}
